package com.weixin.restapi;

import com.weixin.converter.OrderForm2OrderDtoConverter;
import com.weixin.dto.OrderDto;
import com.weixin.enums.ResultEnum;
import com.weixin.exception.SellException;
import com.weixin.form.OrderForm;
import com.weixin.service.BuyerService;
import com.weixin.service.OrderMasterService;
import com.weixin.util.ResultVoUtil;
import com.weixin.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author CJunGod
 * @create 2020-03-01 21:26
 **/
@Slf4j
@RestController
@RequestMapping("/buy/order")
public class BuyerOrderApi {

    @Resource
    private OrderMasterService orderMasterService;

    @Resource
    private BuyerService buyerService;

    //创建订单
    @PostMapping("/create")
    public ResultVo<Map<String, String>> create(@Valid OrderForm OrderForm, BindingResult
            bindingResult){

        if(bindingResult.hasErrors()){
            log.error("{创建订单}参数不正确，orderForm={}",OrderForm);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        OrderDto orderDto = OrderForm2OrderDtoConverter.convert(OrderForm);
        if(orderDto.getOrderDetailList().isEmpty()){
            log.error("{创建订单错误} 购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }

        OrderDto dto = orderMasterService.createOrder(orderDto);
        Map<String, String> map = new HashMap<>();
        map.put("orderId",dto.getMaster().getOrderId());

        return ResultVoUtil.success(map);
    }

    //订单列表
    @GetMapping("/list")
    public ResultVo<List<OrderDto>> list(@RequestParam("openid")String openid,
                                         @RequestParam(value = "page",defaultValue = "0")Integer page,
                                         @RequestParam(value = "size",defaultValue = "10")Integer size){
        if(openid.isEmpty()){
            log.error("{订单查询列表}openid 为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        PageRequest pageRequest = new PageRequest(page, size);
        Page<OrderDto> list = orderMasterService.findList(openid, pageRequest);

        return ResultVoUtil.success(list.getContent());
    }
    //订单详情
    @GetMapping("/detail")
    public ResultVo<OrderDto> detail(@RequestParam("openid")String openid,
                                     @RequestParam("orderId")String orderId){
        OrderDto one = buyerService.findOrderOne(openid, orderId);

        return ResultVoUtil.success(one);
    }
    //取消订单
    @PostMapping("/cancel")
    public ResultVo cancel(@RequestParam("openid")String openid,
                           @RequestParam("orderId")String orderId){
        buyerService.cancelOrder(openid, orderId);

        return ResultVoUtil.success();
    }
}
